/*
 * Copyright (C) 2009 Leandro de Oliveira Aparecido <lehphyro@gmail.com>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.google.code.japa.repository.ret;

import java.lang.reflect.*;
import java.util.*;

import org.slf4j.*;
import org.springframework.core.*;

import com.google.code.japa.annotation.*;
import com.google.code.japa.repository.*;
import com.google.code.japa.repository.query.*;

/**
 * Parser of the {@link com.google.code.japa.annotation.RepositoryReturn} annotation.
 * 
 * @author Leandro Aparecido
 * @since 1.0
 * @see com.google.code.japa.repository.RepositoryAnnotationParser
 * @see com.google.code.japa.repository.ret.RepositoryReturnAnnotationParameterNameDiscoverer
 * @see org.springframework.core.LocalVariableTableParameterNameDiscoverer
 */
public class RepositoryReturnParser extends RepositoryAnnotationParser {

	private static final Logger logger = LoggerFactory.getLogger(RepositoryReturnParser.class);
	
	/**
	 * Creates the parser using default values.
	 */
	public RepositoryReturnParser() {
		PrioritizedParameterNameDiscoverer prioritizedParameterNameDiscoverer = new PrioritizedParameterNameDiscoverer();
		prioritizedParameterNameDiscoverer.addDiscoverer(new RepositoryQueryAnnotationParameterNameDiscoverer());
		prioritizedParameterNameDiscoverer.addDiscoverer(new LocalVariableTableParameterNameDiscoverer());
		setParameterNameDiscoverer(prioritizedParameterNameDiscoverer);
	}

	/**
	 * Parse the annotation of a method.
	 * 
	 * @param method Method to parse query parameters from.
	 * @param arguments Arguments passed on method invocation.
	 * @param returnValue Value returned by method invocation.
	 * @return Parsed parameters or null if the method doesn't have supported annotations.
	 */
	public RepositoryReturnParameters parse(Method method, Object[] arguments, Object returnValue) {
		RepositoryReturnParameters parameters = null;
		RepositoryReturn annotation = method.getAnnotation(RepositoryReturn.class);
		if (annotation != null) {
			if (returnValue instanceof Collection) {
				throw new UnsupportedOperationException("Cannot use collection as parameter of query for return value of method [" + method + "]");
			}
			parameters = new RepositoryReturnParameters();
			parameters.setQueryName(parseQueryName(annotation.value(), annotation.name()));
			parameters.setEmpty(annotation.empty());
			
			Map<String, QueryParameter> queryParameters = new HashMap<String, QueryParameter>();
			queryParameters.putAll(parseQueryParameters(method, arguments));

			if (returnValue == null) {
				logger.warn("Null value returned by method [{}], some parameters for the query [{}] will not be available", method, parameters.getQueryName());
			} else {
				queryParameters.putAll(parseQueryParametersFromExampleBean(returnValue));
			}
			
			parameters.setParameters(queryParameters);
		}
		
		return parameters;
	}
	
}
